//
//  HOExtraAssistantConfirmViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/27/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOExtraAssistantConfirmViewController.h"
#import "HOConstants.h"
#import "HOEditView.h"
#import "HOReplyListViewController.h"
#import "HOStatusTableViewCell.h"
#import "HOReportOrderEntity.h"
#import "HOUtils.h"
#import "HOStatusEntity.h"
#import "HOStaticDataMgr.h"
#import "HOUploadImageView.h"
#import "HORecordView.h"
#import "HORecordMgr.h"
#import "HOExtraAssistantEntity.h"
#import "HOButton.h"

@interface HOExtraAssistantConfirmViewController ()<HOEditViewDelegate>
{
	
}
@property(nonatomic, retain)HOEditView *replyEditView;
@property(nonatomic, retain)HOReportOrderEntity *orderEntity;
@property(nonatomic, retain)HOButton *revokeBtn;
@property(nonatomic, retain)HOButton *checkPassBtn;
@property(nonatomic, retain)UIView *btnView;
@property(nonatomic, retain)NSString *companyId;
@end

@implementation HOExtraAssistantConfirmViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	[self initSubviews];
	[self initBtnViews];
	
	[self registerTapGesture];
	[self registerKeyboard];
	[self createSegMentController];
}

- (void)initSubviews
{
	self.replyEditView = [[[HOEditView alloc] initWithFrame:CGRectMake(0,
																	   0,
																	   self.view.frame.size.width,
																	   80)] autorelease];
	self.replyEditView.limitedCount = 120;
	self.replyEditView.delegate = self;
	self.replyEditView.layer. cornerRadius = 0;
	self.replyEditView.layer.borderWidth = [HOUtils defaultOnePixelConversion];
	[self.replyEditView.layer setMasksToBounds:YES];
	[self.replyEditView setPlaceHolder:NSLocalizedString(@"输入信息", @"输入信息")];	[self.replyEditView getTextView].returnKeyType = UIReturnKeySend;
	[self.view addSubview:self.replyEditView];
}


//创建导航栏分栏控件
-(void)createSegMentController
{
	NSArray *segmentedArray = [NSArray arrayWithObjects:@"庆春院区",@"下沙院区",nil];
	
	UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
	
	segmentedControl.frame = CGRectMake(0, 0, 180, 30);
	
	segmentedControl.selectedSegmentIndex = 0;
	segmentedControl.tintColor = [UIColor colorWithRed:252/255.0 green:245/255.0 blue:248/255.0 alpha:1];
	
	[segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
	
	[self.navigationItem setTitleView:segmentedControl];
}

- (void)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender
{
	//我定义了一个 NSInteger tag，是为了记录我当前选择的是分段控件的左边还是右边。
	NSInteger selecIndex = sender.selectedSegmentIndex;
	switch(selecIndex){
		case 0:
//			self.companyId = kQingChun;
			break;
			
		case 1:
//			self.companyId = kXiasha;
		default:
			break;
	}
}

- (void)initBtnViews
{
	self.btnView = [[[UIView alloc]initWithFrame:CGRectMake(0,
															self.replyEditView.frame.size.height + self.replyEditView.frame.origin.y + HO_HORIZONAL_PADDING,
															self.view.frame.size.width,
															44)] autorelease];
	self.revokeBtn = [HOButton buttonWithType:UIButtonTypeCustom];
	self.revokeBtn.frame = CGRectMake(HO_HORIZONAL_PADDING,
									  2,
									  (self.view.frame.size.width - 3 * HO_HORIZONAL_PADDING) / 2,
									  40);
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
	[self.revokeBtn setTintColor:[UIColor whiteColor]];
	[self.revokeBtn setTitle:@"未维修完成" forState:UIControlStateNormal];
	[self.revokeBtn addTarget:self action:@selector(revokeEntity:) forControlEvents:UIControlEventTouchUpInside];
	[self.revokeBtn setBackgroundImage:[HOUtils createImageWithColor:UIColorFromRGB(0xec6084) forView:self.revokeBtn] forState:UIControlStateNormal];
	[self.revokeBtn setBackgroundImage:[HOUtils createImageWithColor:TEXT_GRAY_DARK_dbdbdb forView:self.revokeBtn] forState:UIControlStateDisabled];
	self.revokeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	self.revokeBtn.layer.cornerRadius = 3;
	self.revokeBtn.tag = 111;
	[self.revokeBtn.layer setMasksToBounds:YES];
	[self.btnView addSubview:self.revokeBtn];
	
	self.checkPassBtn = [HOButton buttonWithType:UIButtonTypeCustom];
	self.checkPassBtn.frame = CGRectMake(self.revokeBtn.frame.size.width + 2 * HO_HORIZONAL_PADDING,
										 self.revokeBtn.frame.origin.y,
										 self.revokeBtn.frame.size.width,
										 40);
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
	[self.checkPassBtn setTintColor:[UIColor whiteColor]];
	[self.checkPassBtn setTitle:@"确认完成" forState:UIControlStateNormal];
	[self.checkPassBtn addTarget:self action:@selector(checkPassEntity:) forControlEvents:UIControlEventTouchUpInside];
	[self.checkPassBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_NORMAL forView:self.checkPassBtn] forState:UIControlStateNormal];
	[self.checkPassBtn setBackgroundImage:[HOUtils createImageWithColor:TEXT_GRAY_DARK_dbdbdb forView:self.checkPassBtn] forState:UIControlStateDisabled];
	self.checkPassBtn.titleLabel.font = [UIFont systemFontOfSize:15];
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	self.checkPassBtn.layer.cornerRadius = 3;
	[self.checkPassBtn.layer setMasksToBounds:YES];
	[self.btnView addSubview:self.checkPassBtn];
	
	[self.view addSubview:self.btnView];
}

- (void)hideKeyboard
{
	[[self.replyEditView getTextView] resignFirstResponder];
}

- (void)onKeyboardWillShow:(NSNotification *)notification
{
	[super onKeyboardWillShow:notification];
	[baseTableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self hideKeyboard];
}
- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"确认外协单", @"确认外协单")];
}

- (void)dealloc
{
	self.replyEditView = nil;
	self.orderEntity = nil;
	self.btnView = nil;
	self.revokeBtn = nil;
	self.checkPassBtn = nil;
	[super dealloc];
}

- (void)revokeEntity:(id)sender
{
	[self requestDataWithStatus:HO_EXTRA_ASS_ORDER_CHECK_UNCOMPLETE];

}

- (void)checkPassEntity:(id)sender
{
	[self requestDataWithStatus:HO_EXTRA_ASS_ORDER_CONFIRMED];

}

- (void)requestDataWithStatus:(int)status
{
	NSString *content = [HOUtils getValideString:[self.replyEditView getText]];
	if(content.length == 0)
		content = @"无";
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_ConfirmExternalComplete,
								  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.extraAssistantEntity.orderId],
								  kHO_FIELD_Content:content,
								  kHO_FIELD_Status: [NSNumber numberWithInt:status]}];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	//	self.orderEntity.status = HO_EXTRA_ASS_ORDER_REPAIRED;
	[self hideKeyboard];
}

#pragma mark -- tool
- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

- (void)didReceiveCommand:(NSString *)action data:(id)data
{
	if([action isEqualToString:kHO_CMD_ReceiveNotificationMessage])
	{
		NSDictionary *dic = [data objectForKey:kHO_FIELD_Extras];
		int type = [[dic valueForKey:kHO_FIELD_SendType] intValue];
		int orderId = [[dic valueForKey:kHO_FIELD_OrderId] intValue];
		if(type == HO_NC_TYPE_ORDER_ASSIGNED && orderId == self.orderEntity.orderId)
		{
//            [self requestData];
		}
	}
}

- (void)cleanup
{
	self.replyEditView.getTextView.text = @"";
	[super cleanup];
}


- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_ConfirmExternalComplete])
	{
		[self sendCommand:kHO_CMD_Report_Detail_Changed data:nil];
		self.view.userInteractionEnabled = NO;
		[self popupHintView:NSLocalizedString(@"提交成功!", @"提交成功!")  inTime:1.5 needCallback:YES];
		[self cleanup];
	}
}

- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

